package com.yh.controller;

import java.io.File;

import java.io.IOException;
import java.io.InputStream;

import java.text.SimpleDateFormat;
import java.util.Date;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;


import com.yh.bean.User;
import com.yh.bean.Usersetting;
import com.yh.service.impl.SettingselfService;

@Controller
public class SettingselfOperate {
	
	@Autowired
	private SettingselfService sss;
	
	//上传图片
	@RequestMapping("/uploadimg")
	@ResponseBody
	public String uploadimg(MultipartFile pic,HttpServletRequest request){
		//获取文件名及其后缀
		String filename=pic.getOriginalFilename();
		//获取文件的最后一个"."所在的index
		int idx = filename.lastIndexOf(".");  
		//截取后缀名
		String extention=filename.substring(idx);
		//新文件名
		Date dt = new Date();  
        SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMddHHmmssSSS");  
        //当前时间作为新文件名
        String time = fmt.format(dt); 
        //获取上传文件保存到的文件夹所在的路径
		String path=request.getServletContext().getRealPath("photos");
		//拼接真实的上传路径
		String realpath=path+"/"+time+extention;
		String result="../photos/"+time+extention;
		try {
			//获取输入流
			InputStream is=pic.getInputStream();
			//获取输出流
			File uploadfile=new File(realpath);
			FileUtils.copyInputStreamToFile(is, uploadfile);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		System.out.println(filename+" .... "+realpath);
		return result;
	}
	
	//查询该用户个人信息
	@RequestMapping("/queryUser")
	@ResponseBody
	public Usersetting queryUser(HttpServletRequest request){
		User u=new User();
		HttpSession session=request.getSession();
		session.setAttribute("user_id",1);
		u.setUserid((int)session.getAttribute("user_id"));
		Usersetting usersetting=sss.queryUser(u);
		return usersetting;
		}

}
